package com.zhifei.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhifei.common.utils.Constant;
import com.zhifei.common.utils.DispatcherUtils;
import com.zhifei.system.domain.EmployeeArchivesEntity;
import com.zhifei.system.domain.EmployeeEntity;
import com.zhifei.system.domain.vo.EmployeeArchivesVo;
import com.zhifei.system.domain.vo.EmployeeVo;
import com.zhifei.system.mapper.EmployeeArchivesMapper;
import com.zhifei.system.service.EmployeeArchivesService;
import com.zhifei.system.service.EmployeeService;
import com.zhifei.system.util.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class EmployeeArchivesServiceImpl extends ServiceImpl<EmployeeArchivesMapper, EmployeeArchivesEntity> implements EmployeeArchivesService {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private EmployeeArchivesMapper employeeArchivesMapper;

    @Override
    public void generateArchives(Date firstDayOfNowMonth,Date lastDayOfNowMonth) {
        List<EmployeeArchivesEntity> employeeArchivesEntityList = new ArrayList<>();
        List<EmployeeEntity> employeeEntityList = employeeService.lambdaQuery().eq(EmployeeEntity::getEmployeeStatus,1).orderByDesc(EmployeeEntity::getCreateTime).list();
        for(EmployeeEntity employeeEntity : employeeEntityList){
            EmployeeArchivesEntity employeeArchivesEntity = new EmployeeArchivesEntity();
            BeanUtils.copyProperties(employeeEntity,employeeArchivesEntity);
            employeeArchivesEntity.setMonth(firstDayOfNowMonth);
            employeeArchivesEntityList.add(employeeArchivesEntity);
        }
        List<Integer> statusList = new ArrayList<>();
        statusList.add(2);statusList.add(3);statusList.add(4);
        List<EmployeeEntity> employeeEntities = employeeService.lambdaQuery().in(EmployeeEntity::getEmployeeStatus,statusList).between(EmployeeEntity::getLeaveProjectDate,firstDayOfNowMonth,lastDayOfNowMonth).orderByDesc(EmployeeEntity::getCreateTime).list();
        for(EmployeeEntity employeeEntity : employeeEntities){
            EmployeeArchivesEntity employeeArchivesEntity = new EmployeeArchivesEntity();
            BeanUtils.copyProperties(employeeEntity,employeeArchivesEntity);
            employeeArchivesEntity.setMonth(firstDayOfNowMonth);
            employeeArchivesEntityList.add(employeeArchivesEntity);
        }
        this.saveBatch(employeeArchivesEntityList);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page page = new Page(Long.parseLong((String)params.get(Constant.PAGE)),Long.parseLong((String)params.get(Constant.LIMIT)));
        String customerName = (String) params.get("customerName");
        Integer employeeStatus = DispatcherUtils.parseInteger(params.get("employeeStatus"));
        String userPost = (String) params.get("userPost");
        String userRank = (String) params.get("userRank");
        String serviceManager = (String) params.get("serviceManager");
        String recruitHr = (String) params.get("recruitHr");
        String name = (String) params.get("name");
        String orderNo = (String) params.get("orderNo");
        String monthStartDate = (String) params.get("monthStartDate");
        String monthEndDate = (String) params.get("monthEndDate");
        String sortField = (String) params.get("sortField");
        String sortType = (String) params.get("sortType");
        List<EmployeeArchivesVo> employeeArchivesVoList = employeeArchivesMapper.queryPage(page,customerName,employeeStatus,userPost,userRank,serviceManager,recruitHr,name,orderNo,monthStartDate,monthEndDate,sortField,sortType);
        page.setRecords(employeeArchivesVoList);
        return new PageUtils(page);
    }
}
